package com.lyncs.ods.modules.setting.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lyncs.ods.common.web.RequestHolder;
import com.lyncs.ods.constant.LanguageEnum;
import com.lyncs.ods.constant.LyncsOdsConstant;
import com.lyncs.ods.modules.setting.model.CountryInfo;
import com.lyncs.ods.modules.setting.model.IndustryInfo;
import com.lyncs.ods.modules.setting.mapper.IndustryInfoMapper;
import com.lyncs.ods.modules.setting.service.IndustryInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 行业信息表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2022-04-20
 */
@Service
public class IndustryInfoServiceImpl extends ServiceImpl<IndustryInfoMapper, IndustryInfo> implements IndustryInfoService {

    @Override
    public List<IndustryInfo> listAvailable(String keyword) {
        List<IndustryInfo> industryInfos = this.lambdaQuery()
                .eq(IndustryInfo::getStatus, LyncsOdsConstant.EnableStatus.ENABLE.getKey())
                .like(StringUtils.isNotEmpty(keyword), IndustryInfo::getName, keyword)
                .list();
        return setName(industryInfos);
    }

    public List<IndustryInfo> setName(List<IndustryInfo> industryInfos) {
        String language = RequestHolder.language();
        //默认中文
        if (CollectionUtils.isEmpty(industryInfos) || LanguageEnum.ZH_CN.getLanguage().equals(language)) {
            return industryInfos;
        }
        return industryInfos.stream().peek(i -> {
            JSONObject nameJson = JSONObject.parseObject(i.getLanguageJson());
            i.setName(nameJson.getString(language));
        }).collect(Collectors.toList());
    }
}
